package com.ithuangyl.common.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;

/**
 * author : itHuang
 * e-mail : ithuangyl@163.com
 * date   : 2020/7/7 22:25
 */
public abstract class BaseFragment extends Fragment implements View.OnClickListener {
    /**
     * Fragment的View加载完毕的标记
     */
    private boolean isViewCreated;
    /**
     * Fragment对用户可见的标记
     */
    private boolean isVisible;

    private View rootView = null;
    protected Activity mActivity;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (Activity) context;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isViewCreated = true;
        prepareRequestData();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            Object layout = getLayout();
            if (layout instanceof Integer) {
                rootView = inflater.inflate((Integer) layout, container, false);
            } else if (layout instanceof View) {
                rootView = (View) layout;
            } else {
                throw new ClassCastException("type of getLayout() must be Integer or View!");
            }
            if (rootView != null) {
                initView(rootView);
            }
        }
        return rootView;
    }

    protected abstract Object getLayout();

    /**
     * 初始化布局资源
     *
     * @return
     */
    protected abstract void initView(View rootView);

    protected void initViewed() {
    }

    ;

    /**
     * 初始化数据
     *
     * @return
     */
    protected void onLazyLoad() {

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        prepareRequestData();
    }

    public boolean prepareRequestData() {
        return prepareRequestData(false);
    }

    public boolean prepareRequestData(boolean forceUpdate) {
        if (getUserVisibleHint() && isViewCreated && (!isVisible || forceUpdate)) {
            onLazyLoad();
            isVisible = true;
            return true;
        }
        return false;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (rootView != null) {
            ((ViewGroup) rootView.getParent()).removeView(rootView);
        }
    }


    /**
     * 模仿jq将找View的方法命名为$
     *
     * @param id
     * @param <T>:泛型的写法省去强转代码
     * @return
     */
    public <T extends View> T $(@IdRes int id) {
        if (rootView != null) {
            return rootView.findViewById(id);
        }
        throw new NullPointerException("rootView is null");
    }

    protected void addClickListener(View view) {
        if (view != null) {
            view.setOnClickListener(this);
        }
    }

    protected void addClickListener(@IdRes int id) {
        if (rootView != null) {
            $(id).setOnClickListener(this);
            return;
        }
        throw new NullPointerException("rootView is null");
    }

    @Override
    public void onClick(View view) {
        onViewClicked(view.getId());
    }

    public void onViewClicked(@IdRes int id) {

    }

    public void addIdsClickListener(ArrayList<Integer> ids) {
        if (ids != null) {
            for (Integer id : ids) {
                addClickListener(id);
            }
        }
    }

    public void addViewsClickListener(ArrayList<View> views) {
        if (views != null) {
            for (View view : views) {
                addClickListener(view);
            }
        }
    }
}
